package com.mlz.mlzmall.ui.activity.act.main;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.UserBean;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.LoginUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.RequestUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.ToastUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyErrorHelper;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyRequestManager;
import com.mlz.mlzmall.ui.activity.act.personal.PersonalActivity;
import com.mlz.mlzmall.ui.inter.ExitActivity;

import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.CrashHandler;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;


/**
 * 功能描述：MyApplication
 * Created by benny on 2016/5/7.
 * ProjectName：mlzMall.
 */
public class MyApplication extends Application {
    private static final String TGA = "MyApplication";
    private static MyApplication instance;
    private static MyApplication mInstance = null;

    public RequestQueue requestQueue;
    public static Context context;
    public ExitActivity exit;
    TelephonyManager telephonyManager;
    public static String deviceId;

    /*
    登录用户，为null时表示非登录状态，全局获取，方便各个Activity判断用户各种信息
     */
    public UserBean loginUser;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        instance = this;
        mInstance = this;
        VolleyRequestManager.init(this);
        requestQueue = Volley.newRequestQueue(this.getApplicationContext());
     //   CrashHandler.getInstance().init(getApplicationContext());//2015.12.05 注释掉，后期要还原 by wls
        //设置全局公共参数
        initImageLoader(getApplicationContext());
        telephonyManager = (TelephonyManager) MyApplication.this.getSystemService(Context.TELEPHONY_SERVICE);
      //  deviceId = telephonyManager.getDeviceId();
    }

    /**
     * 单例，返回一个实例
     *
     * return
     */
    public static MyApplication getInstance() {
        if (instance == null) {
            Log.i("instance", "[ECApplication] instance is null.");
        }
        return instance;
    }


    public static MyApplication getInstace() {
        if (mInstance == null) {
            return new MyApplication();
        }
        return mInstance;

    }

    public static MyApplication getmInstance() {
        return mInstance;
    }

    public static void setmInstance(MyApplication mInstance) {
        MyApplication.mInstance = mInstance;
    }

    public UserBean getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(UserBean loginUser) {
        this.loginUser = loginUser;
    }

    public boolean isLogin() {
        return loginUser != null;
    }

    public void logOut() {
        loginUser = null;
    }

    public static void initImageLoader(Context context) {
        DisplayImageOptions options;
        // 设置选项
        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.u16)
                .showImageOnFail(R.drawable.avatar)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .build();
        ImageLoader imageLoader = ImageLoader.getInstance();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(480, 800)// 缓存在内存的图片的宽和高度
                .discCacheExtraOptions(480, 800, Bitmap.CompressFormat.PNG, 70, null)//CompressFormat.PNG类型，70质量（0-100）
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize(2 * 1024 * 1024) //缓存到内存的最大数据
                .discCacheSize(50 * 1024 * 1024)  //缓存到文件的最大数据
                .discCacheFileCount(1000)//文件数量
                .defaultDisplayImageOptions(options)  //上面的options对象，一些属性配置
                .build();
        imageLoader.init(config);

    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
        }
        return this.requestQueue;
    }

    public static Context getContext() {
        return context;
    }

    public ExitActivity getExit() {
        return exit;
    }

}
